//
//  MYGFeedbackViewController.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/18.
//

import UIKit
import TZImagePickerController
class MYGFeedbackViewController: MYGBaseController {

    @IBOutlet weak var sureBtn: UIButton!
    @IBOutlet weak var contentTF: UITextView!
    @IBOutlet weak var contentCollection: UICollectionView!
    @IBOutlet weak var phoneEmailTF: UITextField!
    
    var dataSource = [FeedBackModel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "帮助与反馈"
        setupUI()
        var add = FeedBackModel()
        add.isAdd = true
        self.dataSource.append(add)
        self.contentCollection.reloadData()
        sureBtn.addCAGradientLayerColor(colors: [UIColor.init(hexString: "#97B3FF").cgColor,
                                                 UIColor.init(hexString: "#4975EC").cgColor],
                                        startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint.init(x: 1, y: 0))
    }
    func setupUI() {
        self.contentTF.placeholderText = "请描述您遇到的问题或您的建议"
        sureBtn.addCAGradientLayerColor(colors: [UIColor.init(hexString: "#97B3FF").cgColor,
                                                 UIColor.init(hexString: "#4975EC").cgColor],
                                        startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint.init(x: 1, y: 0))
        self.contentCollection.register(UINib.init(nibName: "MYGFeedbackCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MYGFeedbackCollectionViewCell")
    }

    @IBAction func tapSure(_ sender: UIButton) {
        var images = ""
        if  let first = self.dataSource.first {
            images = first.url
        }
        if self.phoneEmailTF.text?.count == 0 && self.contentTF.text.count == 0 && images.count == 0 {return}
        MYGLoading.show()
        MYGNetwork<MYGCommonAPI, MYGEmptyModel>.request(target: .feedbackCreate(tel: self.phoneEmailTF.text ?? "", comment: self.contentTF.text ?? "", images: images)) { item in
            MYGLoading.hide()
            MMYGHUD.showToast(text: "提交成功")
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
}

extension MYGFeedbackViewController: UICollectionViewDataSource,UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYGFeedbackCollectionViewCell", for: indexPath) as! MYGFeedbackCollectionViewCell
        let obj = self.dataSource[indexPath.row]
        if obj.isAdd {
            cell.icon.image = UIImage.init(named: "Find/add_image")
        }else {
            cell.icon.sd_setImage(with: URL(string: obj.url))
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let obj = self.dataSource[indexPath.row]
        if obj.isAdd {
            let picker = TZImagePickerController(maxImagesCount: 1, delegate: self)!
            picker.allowPickingVideo = false
            picker.allowTakePicture = true
            self.present(picker, animated: true)
        }
    }
    
}

extension MYGFeedbackViewController:TZImagePickerControllerDelegate {
    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool) {
        if let image = photos.first {
            MYGLoading.show()
            MMYGNetwork<MYGCommonAPI, MYGEmptyModel>.request(target: .uploadOssFiles(image: image)) { _ in
                MYGLoading.hide()
            } stringBlock: { text in
              var model = FeedBackModel()
                model.url = text
                self.dataSource.append(model)
                //默认只能添加一张
                self.dataSource = self.dataSource.filter({ model in
                    return model.isAdd == false
                })
                self.contentCollection.reloadData()
            } failed: { _, error in
                MYGLoading.hide()
                MMYGHUD.showToast(text: error)
            }
        }
    }
}

struct FeedBackModel {
    var url = ""
    var isAdd = false
}
